package com.flainsky.nirvanaplayer.coder;

import java.io.IOException;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
import com.flainsky.nirvanaplayer.lib.objects.Movie;



public class MovieObjectXML {
	
	public static List<String> StringDecode(String obj)
	{
		List<String> list = new ArrayList<String>();
		try
		{
			StringReader read = new StringReader(obj);
	        //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
	        InputSource source = new InputSource(read);
	        //创建一个新的SAXBuilder
	        SAXBuilder sb = new SAXBuilder();
		    //通过输入源构造一个Document
		    Document doc = sb.build(source);
		    //取的根元素
		    Element root = doc.getRootElement();
		    //得到根元素所有子元素的集合
		    @SuppressWarnings("rawtypes")
			List jiedian = root.getChildren();
		    //获得XML中的命名空间（XML中未定义可不写）
		    //Namespace ns = root.getNamespace();
		    Element et = null;
		    for(int i=0;i<jiedian.size();i++){
		    	et = (Element) jiedian.get(i);//循环依次得到子元素
		    	Element cet = (Element) et.getChildren().get(0);
		    	list.add(cet.getText());
		    }
		    return list;
		}
		catch(JDOMException ex)
		{
			ex.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public static List<Movie> MovieDecode(String obj)
	{
		int mId = 0;				//
		String mTitle = "";
		String mArea = "";
		String mCategory = "";
		int mYear = 2000;
		int mNum_episode = 1;
		String mDirector = "";
		List<String> mLeading_role;
		String mIntro = "";
		float mComment = 2.5f;
		String mSimage = "";
		String mBimage = "";
		int mPlaytimes = 0;
		Date mUpload_time;
		int mLenth = 60;
		int mType = 0;
		String mPlay_url = "";
		
		List<Movie> movies = new ArrayList<Movie>();
		Movie movie;
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
		    StringReader read = new StringReader(obj);
	        //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
	        InputSource source = new InputSource(read);
	        //创建一个新的SAXBuilder
	        SAXBuilder sb = new SAXBuilder();
		    //通过输入源构造一个Document
		    Document doc = sb.build(source);
		    //取的根元素
		    Element root = doc.getRootElement();
		    //得到根元素所有子元素的集合
		    @SuppressWarnings("rawtypes")
			List jiedian = root.getChildren();
		    //获得XML中的命名空间（XML中未定义可不写）
		    //Namespace ns = root.getNamespace();
		    Element et = null;
		    for(int i=0;i<jiedian.size();i++){
		        et = (Element) jiedian.get(i);//循环依次得到子元素
		        mId = Integer.valueOf(et.getChild("id").getText());
		        mTitle = et.getChild("title").getText();
		        mArea = et.getChild("area").getText();
		        mCategory = et.getChild("category").getText();
		        mYear = Integer.valueOf(et.getChild("time").getText());
		        mNum_episode = Integer.valueOf(et.getChild("num_episode").getText());
		        mDirector = et.getChild("director").getText();
		        mLeading_role = new ArrayList<String>();
		        String roles = et.getChild("leading_role").getText();
		        String[] rolesArray = roles.split(";");
		        for(int j = 0 ; j<rolesArray.length;j++)
		        {
		        	mLeading_role.add(rolesArray[j]);
		        }
		        mIntro = et.getChild("intro").getText();
		        mComment = Float.valueOf(et.getChild("comment").getText());
		        mSimage = et.getChild("simage").getText();
		        mBimage = et.getChild("bimage").getText();
		        mPlaytimes = Integer.valueOf(et.getChild("play_times").getText());
		        try {
					mUpload_time = format.parse(et.getChild("upload_time").getText().substring(0, 19));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					mUpload_time = new Date();
					e.printStackTrace();
				}
		        mLenth = Integer.valueOf(et.getChild("lenth").getText());
		        mType = Integer.valueOf(et.getChild("type").getText());
		        mPlay_url = et.getChild("play_url").getText();
		        
		        movie = new Movie(mId, mTitle, mArea, mCategory, mYear, mNum_episode, mDirector, mLeading_role, mIntro, mComment, mSimage, mBimage, mPlaytimes, mUpload_time, mLenth, mType, mPlay_url);
		        movies.add(movie);
		    }
		    return movies;
		}
		catch(JDOMException ex)
		{
			ex.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
